/**
 * @file trade/edit.js
 *  实例操作流水编辑详情
 * @author
 */

 import React, {Component,PropTypes} from 'react';
 import {connect} from 'dva';
 import {routerRedux,withRouter } from 'dva/router';
 import { Form, Input, DatePicker, Select, Button, Icon, Row, Col, Breadcrumb, Card } from 'antd';
 import styles from './edit.less';

 const FormItem = Form.Item;
 const Option = Select.Option;
 const dateFormat = 'YYYYMMDD';
 let obj;
 const FORM_ITEM_LAYOUT = {
	labelCol: {
		xl:{
			span:8
		},
		lg:{
			span:8
		},
		xs: {
			span: 4,
		},
		sm: {
			span: 4,
		},
	},
	wrapperCol: {
		xl:{
			span:8
		},
		lg:{
			span:8
		},
		sm: {
			span: 10,
		},
		xs: {
			span: 8,
		},
	},
};

const Tradeedit = ({
	dispatch,
	trade,
  location: {
		pathname,
		query
	},
	form: {
		getFieldDecorator,
		validateFields,
		getFieldsValue },
	})=>{
    const {tradelist,begindate, enddate,} = trade;
    console.log("trade>>>>>",trade);
    // const pathname = location.pathname;
    const pathArr = pathname.split("/")[5].split("-");
    tradelist.forEach((item) => {
			if (item.tradeDay === pathArr[0]
				&& item.fundId === pathArr[1]
				&& item.combiNo === pathArr[2]
				&& item.stockCode === pathArr[3]
				&& item.tradeDirectionText === decodeURIComponent(pathArr[4])
				&& +item.dealAmount === +pathArr[5]
				&& item.tradeTypeText === decodeURIComponent(pathArr[6]))  {
				obj = item;
			}
		});
		function handleSubmit(e) {
			e.preventDefault();
			validateFields((err, fieldsValue) => {
        console.log("fieldsValue>>>>>",fieldsValue);
				if (err) {
					return;
				}
        // if (+obj.avgBasisSpread === +fieldsValue.avgBasisSpread
  			// 	&& +obj.dealAmount === +fieldsValue.dealAmount) {
  			// 	return;
  			// }
			    fieldsValue.avgBasisSpread = +fieldsValue.avgBasisSpread;
			    fieldsValue.dealAmount = +fieldsValue.dealAmount;
				dispatch({
					type: 'trade/saveEdit',
					payload: {
            ...obj,
            ...fieldsValue,

					}
				})
			})

		}
		function handleBacktrack(){
			dispatch(routerRedux.push({
        pathname:`/achievement/hedge/instance`,
				query:{
          table:"iftrade",
          currentpage:1,
          pagesiz:15,
					begindate:begindate,
          enddate:enddate
				}
			}))
		}
		return (
			<div>
				<div className={styles.bread}>
					<Breadcrumb separator=">">
						<Breadcrumb.Item  onClick={() => handleBacktrack()} className={styles.breadcr}>实例操作流水</Breadcrumb.Item>
						<Breadcrumb.Item  >修改</Breadcrumb.Item>
					</Breadcrumb>
				</div>
				<div className="page-tradeedit-home content-inner" style={{ background: '#ECECEC', padding: '30px' }}>
					<Card>
					  <Form onSubmit={handleSubmit} >
              <FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="交易日期"
  					  >
						    {getFieldDecorator(
    							'tradeDay',
    							{
    								initialValue: obj.tradeDay,
    							},
    						  )(
			            <Input disabled />,
						    )}
				      </FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="基金号"
    					>
    						{getFieldDecorator(
    							'fundId',
    							{
    								initialValue: obj.fundId,
    							},
    						)(
    							<Input disabled />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="组合号"
    					>
    						{getFieldDecorator(
    							'combiNo',
    							{
    								initialValue: obj.combiNo,
    							},
    						)(
    							<Input disabled />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="组合名称"
    					>
    						{getFieldDecorator(
    							'combiName',
    							{
    								initialValue: obj.combiName,
    							},
    						)(
    							<Input disabled />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="实例号"
    					>
    						{getFieldDecorator(
    							'instanceId',
    							{
    								initialValue: obj.instanceId,
    							},
    						)(
    							<Input disabled />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="证券名称"
    					>
    						{getFieldDecorator(
    							'stockCode',
    							{
    								initialValue: obj.stockCode,
    							})(
    								<Input disabled />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="交易类型"
    					>
    						{getFieldDecorator(
    							'tradeDirection',
    							// 'select',
    							{
    								initialValue: obj.tradeDirection,
    							})(
    								<Select placeholder="请选择您的交易类型" disabled>
    									<Option value="BuyOpen">买入开仓</Option>
    									<Option value="SellOpen">卖出开仓</Option>
    									<Option value="BuyClose">买入平仓</Option>
    									<Option value="SellClose">卖出平仓</Option>
    								</Select>,
    							)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}

    						label="平均基差"
    					>
    						{getFieldDecorator(
    							'avgBasisSpread',
    							{
    								initialValue: obj.avgBasisSpread,
    								rules: [
    									{
                        required: true, message: '请输入平均基差',
                        pattern: /^[-]?\d+(?:\.\d+)?$/, whitespace: true, message: '请输入数字!',
                      },
    								],
    							},
    						)(
    							<Input />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}

    						label="数量"
    					>
    						{getFieldDecorator(
    							'dealAmount',
    							{
    								initialValue: obj.dealAmount,
    								rules: [
    									{
      									required: true, message: '请输入数量',
    										pattern: /^[-]?\d+(?:\.\d+)?$/, whitespace: true, message: '请输入数字!',
    									},
    								],
    							},
    						)(
    							<Input />,
    						)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="汇总类型"
    					>
    						{getFieldDecorator(
    							'tradeType',
    							// 'select',
    							{
    								initialValue: obj.tradeType,
    							},
    						)(
    							<Select placeholder="请选择您的汇总类型" disabled>
    								<Option value="Transfer">划转</Option>
    								<Option value="Trade">交易</Option>
    							</Select>,
    							)}
    					</FormItem>
    					<FormItem
    						{...FORM_ITEM_LAYOUT}
    						label="备注"
    					>
    						{getFieldDecorator(
    							'note',
    							{
    								initialValue: obj.note,
    							},
    						)(
    							<Input type="textarea" rows={2} />,
    						)}
    					</FormItem>
    					<FormItem
    						wrapperCol={{
    							xl: { span: 14, offset: 10 },
    							xs: { span: 12, offset: 10 },
    							sm: { span: 8, offset: 10 },
    						}}
    					>
    						<Row>
    							<Col span={6}>
    								<Button type="primary" htmlType="submit" size="large" to="/" >提交</Button>
    							</Col>
    							<Col span={6}>
    								<Button type="primary" size="large" onClick={() => handleBacktrack()} >取消</Button>
    							</Col>
    						</Row>
					     </FormItem>
					   </Form>
					</Card>
				</div>
			</div>
		)
}

Tradeedit.propTypes = {
	form: PropTypes.object.isRequired,
}

export default connect(({trade})=>({trade}))(Form.create()(Tradeedit))
